package sedh.project
import grails.plugins.springsecurity.Secured;

class ProgramaController {

	static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

	def index = {
		redirect(action: "list", params: params)
	}
	
	
	def searchPrograma = {
		def list = Programa.findAllByNomeIlike(wrapSearchParm(params.searchvalue), [sort:'nome',order:'asc'])
		render(template:'searchResult', model:[searchresults : list])
	}
	def String wrapSearchParm(value) {
		'%'+value+'%'
	}
	
	@Secured(['IS_AUTHENTICATED_REMEMBERED'])
	def list = {
		def mun = params.get("mun") ? params.get("mun") : 1

		params.sort = "nome"
		params.order = "asc"

		def programaInstance = Programa.get(params.id)
		def alocMunProgInstance = AlocadorMunProg.getAll()

		if (!programaInstance) {
			flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'programa.label', default: 'Programa'), params.id])}"
			if(flash.message.contains("1")) flash.message = ""
			[programaInstanceList: Programa.list(params), programaInstanceTotal: Programa.count()]
		}

		else {

			[mun:mun, alocMunProgInstance:alocMunProgInstance, programaInstance: programaInstance, programaInstanceList: Programa.list(params),
						programaInstanceTotal: Programa.count()]
		}
	}

	@Secured(['IS_AUTHENTICATED_REMEMBERED'])
	def create = {
		def programaInstance = new Programa()
		programaInstance.properties = params
		return [programaInstance: programaInstance]
	}

	def save = {
		params.nome = params.nome.toLowerCase().split(' ').collect{ it.capitalize() }.join(' ')
		def programaInstance = new Programa(params)
		if (programaInstance.save(flush: true)) {
			flash.message = "${message(code: 'default.created.message', args: [message(code: 'programa.label', default: 'Programa'), programaInstance.id])}"
			redirect(action: "show", id: programaInstance.id)
		}
		else {
			render(view: "create", model: [programaInstance: programaInstance])
		}
	}
	
	@Secured(['IS_AUTHENTICATED_REMEMBERED'])
	def show = {
		redirect(action: "list", id: params.id)
	}

	@Secured(['IS_AUTHENTICATED_REMEMBERED'])
	def edit = {
		def programaInstance = Programa.get(params.id)
		if (!programaInstance) {
			flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'programa.label', default: 'Programa'), params.id])}"
			redirect(action: "list")
		}
		else {
			return [programaInstance: programaInstance]
		}
	}

	def update = {
		def programaInstance = Programa.get(params.id)

		//deleta programa
		if (programaInstance) {
			if (params.version) {
				def version = params.version.toLong()
				if (programaInstance.version > version) {

					programaInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [
						message(code: 'programa.label', default: 'Programa')]
					as Object[], "Another user has updated this Programa while you were editing")
					render(view: "edit", model: [programaInstance: programaInstance])
					return
				}
			}
			programaInstance.properties = params
			if (!programaInstance.hasErrors() && programaInstance.save(flush: true)) {
				flash.message = "${message(code: 'default.updated.message', args: [message(code: 'programa.label', default: 'Programa'), programaInstance.id])}"
				redirect(action: "show", id: programaInstance.id)
			}
			else {
				render(view: "edit", model: [programaInstance: programaInstance])
			}
		}
		else {
			flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'programa.label', default: 'Programa'), params.id])}"
			redirect(action: "list")
		}
	}
	
	@Secured(['IS_AUTHENTICATED_REMEMBERED'])
	def delete = {
		def programaInstance = Programa.get(params.id)

		def programasAlocados = AlocadorMunProg.findAllByPrograma(programaInstance)

		//deleta as alocacoes
		for(AlocadorMunProg aloc : programasAlocados) {
			if (aloc) {

				try {
					aloc.delete(flush: true)
					flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'programa.label', default: 'Programa'), params.id])}"

				}
				catch (org.springframework.dao.DataIntegrityViolationException e) {
					flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'programa.label', default: 'Programa'), params.id])}"
				}
			}
		}

		if (programaInstance) {
			try {
				programaInstance.delete(flush: true)
				flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'programa.label', default: 'Programa'), params.id])}"

			}
			catch (org.springframework.dao.DataIntegrityViolationException e) {
				flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'programa.label', default: 'Programa'), params.id])}"
				redirect(action: "show", id: params.id)
			}
		}
		else {
			flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'programa.label', default: 'Programa'), params.id])}"
			redirect(action: "list")
		}
	}
}
